package com.ruoyi.subject.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.subject.domain.QbSubCollect;
import com.ruoyi.subject.service.IQbSubCollectService;
import com.ruoyi.subject.util.QbSubjectUtil;
import com.ruoyi.user.service.IQbUserWxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 我的收藏Controller
 *
 * @author lzhj
 * @date 2021-01-27
 */
@Api(tags = "我的收藏")
@RestController
@RequestMapping("/qb/sub/collect")
public class QbSubCollectController extends BaseController {
    @Autowired
    private IQbSubCollectService qbSubCollectService;

    @Autowired
    private IQbUserWxService qbUserWxService;

    /**
     * 查询我的收藏列表
     */
    @ApiOperation(value = "查询我的收藏列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "userId", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "openId", value = "openid", dataType = "string", paramType = "query"),
    })
    @PostMapping("/list")
    public AjaxResult list(long userId, String openId) {
        //验证用户信息
        if (!qbUserWxService.validataUserInfo(userId, openId)) {
            return QbSubjectUtil.getWxValidataFailMag();
        }
        List<QbSubCollect> list = qbSubCollectService.selectQbSubCollectListByUserId(userId);
        return AjaxResult.success("查询成功", list);
    }


    /**
     * 新增我的收藏
     */
    @ApiOperation(value = "新增我的收藏,新增一条数据")
    @Log(title = "我的收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QbSubCollect qbSubCollect) {
        //验证用户信息
        if (!qbUserWxService.validataUserInfo(qbSubCollect.getUserId(), qbSubCollect.getOpenId())) {
            return QbSubjectUtil.getWxValidataFailMag();
        }
        AjaxResult ajaxResult = new AjaxResult();
        //判断是否存在改收藏
        Long id = qbSubCollectService.selectQbSubCollectBy(qbSubCollect);
        if (id != null) {
            ajaxResult =  AjaxResult.success("已收藏");
        }else {
            ajaxResult = toAjax(qbSubCollectService.insertQbSubCollect(qbSubCollect));
            id  = qbSubCollect.getId();
        }
        ajaxResult.put("id", id);
        return ajaxResult;
    }

    /**
     * 修改我的收藏
     */
    @ApiOperation(value = "修改我的收藏，根据收藏id", hidden = true)
    @Log(title = "我的收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QbSubCollect qbSubCollect) {
        return toAjax(qbSubCollectService.updateQbSubCollect(qbSubCollect));
    }

    /**
     * 删除我的收藏
     */
    @ApiOperation(value = "删除我的收藏，根据收藏id", hidden = true)
    @Log(title = "我的收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(qbSubCollectService.deleteQbSubCollectByIds(ids));
    }

    /**
     * 删除我的收藏
     */
    @ApiOperation(value = "删除我的收藏，根据收藏id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "userId", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "openId", value = "openid", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "ids", value = "收藏id", dataType = "long", paramType = "query"),
    })
    @Log(title = "我的收藏", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(long userId, String openId, @RequestParam(value = "ids") Long[] ids) {
        if (!qbUserWxService.validataUserInfo(userId, openId)) {
            return QbSubjectUtil.getWxValidataFailMag();
        }
        return toAjax(qbSubCollectService.deleteQbSubCollectByIds(ids));
    }
}
